<?php
namespace app\admin\controller;
use think\Session;
use think\Loader;
use app\model\Items;

class ItemsController extends AuthController
{
    protected $items;

	protected function _initialize(){
        parent::_initialize();
		$this->items = new Items();
	}

    /**
     * 订单列表
     * @return mixed
     */
	public function index(){
	    $title = $this->request->param('title');
        $cate_id = $this->request->param('cate');

        $items = $this->items;

        if(!is_null($title)&&($title != '')){
            $items = $this->items->where('title','like','%'.$title.'%');
        }

        if(!is_null($cate_id)&&($cate_id != '')){
            $items = $this->items->where('cate_id',$cate_id);
        }

        $result = $items->order('id desc')->paginate(20);
        $this->assign('list',$result);
        return $this->fetch();
	}

    /**
     * 订单删除
     * @return array
     */
    public function del(){
        $id = $this->request->param('id');
        if (!$this->request->has('id')) {
            $this->error('不存在ID,删除失败');
        }
        return Items::destroy(['id' => $id]) ? $this->success('删除成功') :$this->error('删除失败');
    }

    /**
     * 订单状态编辑
     * @return array
     */
    public function editStatus(){
        if($this->request->isPost()) {
            $data = [
                'id' => $this->request->post('id'),
                'status' => $this->request->post('status'),
            ];
            if (!$this->request->has('id')) {
                return msgReturn(false,'编辑失败',null);
            }
            $order = Order::get($data['id']);
            if($order->admin_id != Session::get('admin_id') &&($order->admin_id != 0))
                return msgReturn(false,'只能编辑自己处理的订单','error');

            return $this->order->save(['status' => $data['status']],['id' => $data['id']]) ? msgReturn(true,'编辑成功','success') : msgReturn(false,$this->order->getError(),null);
        }
    }

    /**
     * 订单评价
     */
    public function review(){

    }

}